//
//  SEGWindmillRefreshCommonFooter.m
//  RefreshDemo
//
//  Created by Clavis on 2019/1/4.
//  Copyright © 2019 Clavis. All rights reserved.
//

#import "SEGWindmillRefreshCommonFooter.h"

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif
#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "Commom/SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/UIImage+YYAdd.h>)
#import <Commom/UIImage+YYAdd.h>
#else
#import "UIImage+YYAdd.h"
#endif

@interface SEGWindmillRefreshCommonFooter ()
/** 所有状态对应的文字 */
@property (strong, nonatomic) NSMutableDictionary *stateTitles;
@property (nonatomic, strong) UIImageView *pullImageView;
@end

@implementation SEGWindmillRefreshCommonFooter

#pragma - 公共方法
- (void)setTitle:(NSString *)title forState:(SEGRefreshState)state
{
    if (title == nil) return;
    self.stateTitles[@(state)] = title;
    self.stateLabel.text = self.stateTitles[@(self.state)];
}

- (NSString *)titleForState:(SEGRefreshState)state {
    return self.stateTitles[@(state)];
}

#pragma - 私有方法
- (void)setIsAutoRefresh:(BOOL)isAutoRefresh {
    [super setIsAutoRefresh:isAutoRefresh];
    self.stateLabel.hidden = self.isAutoRefresh;
    self.pullImageView.hidden = self.isAutoRefresh;
}

#pragma - getter & setter
- (UIImageView *)pullImageView {
    if (!_pullImageView) {
        _pullImageView = ({
            UIImageView *imageView = [[UIImageView alloc] init];
            NSMutableArray *imageArray = [NSMutableArray array];
            for (NSInteger i = 1; i <= 71; i++) {
//                NSString *path = [[[NSBundle mainBundle] pathForResource:@"SEGResources" ofType:@"bundle"] stringByAppendingPathComponent:[NSString stringWithFormat:@"pull_refresh_%02zd",i]];
//                UIImage *image = [UIImage imageWithContentsOfFile:path];
                
                NSString *imageName = [NSString stringWithFormat:@"pull_refresh_%02zd",i];
                UIImage *image = [[UIImage imageWithContentsOfFile:SEGPicName(imageName)] imageByTintColor:kColor_T];
                if (image) {
                    [imageArray addObject:image];
                }
            }
            imageView.image = [[UIImage imageWithContentsOfFile:SEGPicName(@"pull_refresh_01")] imageByTintColor:kColor_T];
            imageView.animationImages = imageArray;
            imageView.animationDuration = 2;
            [imageView startAnimating];
            imageView;
        });
    }
    return _pullImageView;
}

#pragma mark - 重写父类的方法
- (void)prepare {
    [super prepare];
    
    [self addSubview:self.pullImageView];
    self.pullImageView.frame = CGRectMake(50, 20, 20, 20);
    
    self.stateLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        lable.textColor = kColor_3;
        lable.font = [UIFont systemFontOfSize:11];
        lable;
    });
    [self addSubview:self.stateLabel];
    
    self.stateTitles = [NSMutableDictionary dictionary];
    
    // 初始化文字
    [self setTitle:@"上拉加载更多" forState:SEGRefreshStateIdle];
    [self setTitle:@"松开后刷新" forState:SEGRefreshStatePulling];
    [self setTitle:@"正在刷新..." forState:SEGRefreshStateRefreshing];
    [self setTitle:@"没有更多数据了" forState:SEGRefreshStateNoMoreData];
    
    // 添加约束
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(10);
        make.centerY.equalTo(self);
    }];
    
    [self.pullImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.stateLabel);
        make.right.equalTo(self.stateLabel.mas_left).mas_offset(-6);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
}

- (void)placeSubviews {
    [super placeSubviews];
    if (self.stateLabel.constraints.count) return;
}

- (void)setState:(SEGRefreshState)state {
    SEGRefreshCheckState
    
    // 设置状态文字
    self.stateLabel.text = self.stateTitles[@(state)];
}

@end
